﻿using Newtonsoft.Json;
using Omabar.BLL;
using Omabar.Common;
using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace Omabar.CMS.Handler
{
    /// <summary>
    /// PackageAdd 的摘要说明
    /// </summary>
    public class PackageAdd : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            RESP_Info info = new RESP_Info();
            try
            {
                context.Response.ContentType = "text/plain";
                string ROUTE_ID = context.Request["ROUTE_ID"];
                string PACKAGE_ID = context.Request["PACKAGE_ID"];
                string PACKAGE_DES = context.Request["PACKAGE_DES"];
                string PACKAGE_INCLUDE = context.Request["PACKAGE_INCLUDE"];
                string PACKAGE_EXCLUDE = context.Request["PACKAGE_EXCLUDE"];

                PACKAGE package = null;
                Package_BLL bll = new Package_BLL();

                if (string.IsNullOrEmpty(PACKAGE_ID))
                {
                    package = new PACKAGE()
                    {
                        PACKAGE_ID = Util.GetUID(),
                        PACKAGE_DES = PACKAGE_DES,
                        PACKAGE_INCLUDE = PACKAGE_INCLUDE,
                        PACKAGE_EXCLUDE = PACKAGE_EXCLUDE
                    };

                    info = bll.Add(package);
                }
                else
                {
                    package = new PACKAGE()
                    {
                        PACKAGE_ID = PACKAGE_ID,
                        PACKAGE_DES = PACKAGE_DES,
                        PACKAGE_INCLUDE = PACKAGE_INCLUDE,
                        PACKAGE_EXCLUDE = PACKAGE_EXCLUDE
                    };

                    info = bll.Update(package);
                }

                Route_Package_BLL rpBLL = new Route_Package_BLL();
                rpBLL.ExecuteSQL(string.Format("delete from ROUTE_PACKAGE where ROUTE_ID='{0}'", ROUTE_ID));
                rpBLL.Add(new ROUTE_PACKAGE()
                {
                    ROUTE_ID = ROUTE_ID,
                    PACKAGE_ID = package.PACKAGE_ID
                });
            }
            catch (Exception ex)
            {
                Tom.Net.Log.LogManager.Error("添加路线套餐失败", ex);
                info.Success = false;
                info.Title = "错误";
                info.Tip = "<h5><p>添加路线套餐失败</p></h5>";
                info.Url = "PackageAdd.aspx";
            }

            context.Response.Write(JsonConvert.SerializeObject(info));
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}